package com.king.photo.util;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import com.yxr.affairs.R;
import com.yxr.affairs.api.model.ResultEntity;
import com.yxr.affairs.api.model.child.Login;
import com.yxr.affairs.base.BaseSwipeBackActivity;


public class AlbumActivity extends BaseSwipeBackActivity {
	//显示手机里的所有图片的列表控件
	private GridView gridView;
	//当手机里没有图片时，提示用户没有图片的控件
	private TextView tv;
	//gridView的adapter
	private AlbumGridViewAdapter gridImageAdapter;
	//完成按钮
	private Button okButton;
	private Intent intent;
	// 预览按钮 
	private Button preview;
	private ArrayList<ImageItem> dataList;
	private AlbumHelper helper;
	public static List<ImageBucket> contentList;

	@Override
	protected int attachLayoutRes() {
		return R.layout.plugin_camera_album;
	}

	@Override
	protected void initViews() {

		init();
		initListener();
	}

	@Override
	protected void updateViews(boolean isRefresh) {

	}




	@Override
	protected void onError_(Throwable e) {

	}

	@Override
	protected void onCancel_() {

	}

	// 预览按钮的监听
	private class PreviewListener implements OnClickListener {
		public void onClick(View v) {
			if (Bimp.tempSelectBitmap.size() > 0) {
				intent.putExtra("position", "1");
				intent.setClass(AlbumActivity.this, GalleryActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.hold, R.anim.slide_right_exit);

			}
		}

	}

	// 完成按钮的监听
	private class AlbumSendListener implements OnClickListener {
		public void onClick(View v) {
			finish();
		}

	}
	
	// 初始化，给一些对象赋值
	private void init() {
		helper = AlbumHelper.getHelper();
		helper.init(getApplicationContext());
		
		contentList = helper.getImagesBucketList(true);
		dataList = new ArrayList<ImageItem>();
		for(int i = 0; i<contentList.size(); i++){
			dataList.addAll(contentList.get(i).imageList );
		}
		
		findViewById(R.id.back).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				_exit();
			}
		});
		
		preview = (Button) findViewById(R.id.preview);
		preview.setOnClickListener(new PreviewListener());
		
		intent = getIntent();
		gridView = (GridView) findViewById(R.id.myGrid);
		gridImageAdapter = new AlbumGridViewAdapter(this,dataList, Bimp.tempSelectBitmap);
		gridView.setAdapter(gridImageAdapter);
		
		tv = (TextView) findViewById(R.id.myText);
		gridView.setEmptyView(tv);
		okButton = (Button) findViewById(R.id.ok_button);
		
		findViewById(R.id.bottom_layout).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//
			}
		});
	}


	private void _exit() {
		finish();
	}

	@Override
	public void finish() {
		super.finish();
		overridePendingTransition(R.anim.hold, R.anim.slide_right_exit);
	}
	@Override
	protected void onResume() {
		super.onResume();
		gridImageAdapter.notifyDataSetChanged();
		isShowOkBt();
	}

	private void initListener() {

		gridImageAdapter
				.setOnItemClickListener(new AlbumGridViewAdapter.OnItemClickListener() {

					@Override
					public void onItemClick(final ToggleButton toggleButton,
							int position, boolean isChecked,Button chooseBt) {
						if (Bimp.tempSelectBitmap.size() >= PublicWay.num) {
							toggleButton.setChecked(false);
							chooseBt.setVisibility(View.GONE);
							if (!removeOneData(dataList.get(position))) {
								Toast.makeText(AlbumActivity.this, "超出可选图片张数", Toast.LENGTH_SHORT).show();
							}
							return;
						}
						if (isChecked) {
							chooseBt.setVisibility(View.VISIBLE);
							Bimp.tempSelectBitmap.add(dataList.get(position));
							okButton.setText("完成" + "(" + Bimp.tempSelectBitmap.size()
									+ "/"+PublicWay.num+")");
						} else {
							Bimp.tempSelectBitmap.remove(dataList.get(position));
							chooseBt.setVisibility(View.GONE);
							okButton.setText("完成" + "(" + Bimp.tempSelectBitmap.size() + "/"+PublicWay.num+")");
						}
						isShowOkBt();
					}
				});

		okButton.setOnClickListener(new AlbumSendListener());

	}

	private boolean removeOneData(ImageItem imageItem) {
			if (Bimp.tempSelectBitmap.contains(imageItem)) {
				Bimp.tempSelectBitmap.remove(imageItem);
				okButton.setText("完成" + "(" +Bimp.tempSelectBitmap.size() + "/"+PublicWay.num+")");
				return true;
			}
		return false;
	}
	
	public void isShowOkBt() {
		if (Bimp.tempSelectBitmap.size() > 0) {
			okButton.setText("完成" + "(" + Bimp.tempSelectBitmap.size() + "/"+PublicWay.num+")");
			
			preview.setPressed(true);
			okButton.setPressed(true);
			
			preview.setEnabled(true);
			okButton.setEnabled(true);
		} else {
			okButton.setText("完成" + "(" + Bimp.tempSelectBitmap.size() + "/"+PublicWay.num+")");
			
			preview.setPressed(false);
			okButton.setPressed(false);
			
			preview.setEnabled(false);			
			okButton.setEnabled(false);
		}
	}

	@Override
	public void onBackPressed() {
		return;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			_exit();
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	protected void onRestart() {
		isShowOkBt();
		super.onRestart();
	}
}
